import {
  login,
  logout
} from '@/api/login'
import request from '@/utils/request'
// import {
//   getToken,
//   setToken,
//   removeToken
// } from '@/utils/auth'
// import sesto from '@/storage/sessionstorage.js'
// import router from '@/router/index.js'
const user = {
  state: {
    // 用户名和密码
    token: '', // 从cookie中获取token
    name: '',
    avatar: '',
    menuList: [],
    basicInfo: {},
    // 获取当前课程信息
    courseInfo: {},
    otherInfo: {}
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_MENUList: (state, menuList) => {
      state.menuList = menuList
    },
    SET_BASICINFO: (state, basicInfo) => {
      state.basicInfo = basicInfo
    },
    SET_COURSEINFO: (state, courseInfo) => {
      state.courseInfo = courseInfo
    },
    SET_OTHERINFO: (state, otherInfo) => {
      state.otherInfo = otherInfo
    }
  },

  actions: {
    // 登录
    Login({
      commit
    }, userInfo) {
      const username = userInfo.username.trim()
      const password = userInfo.password.trim()
      return new Promise((resolve, reject) => {
        login(username, password).then(res => {
          // 返回一个登陆权限认证存起来
          const token = {
            code: res.code,
            msg: res.msg
          }
          // setToken(token.Authorization)  // cookies 处的token
          commit('SET_TOKEN', token)
          console.log(token)
          resolve()
        })
          .catch(error => {
            const token = {
              code: 4000,
              msg: 'failed'
            }
            // setToken(token.Authorization)  // cookies 处的token
            commit('SET_TOKEN', token)
            console.log(token)
            reject(error)
          })
      })
    },

    // 获取用户信息(包括 roles 和 其他 用户信息)
    // GetInfo({
    //   commit
    // }, {
    //   state,
    //   info
    // }) {
    //   return new Promise((resolve, reject) => {
    //     if (state === 1) {
    //       getInfo(state).then(res => {
    //         // console.log(res.data)
    //         if (res.data.menuList.length > 0) { // 验证返回的roles是否是一个非空数组
    //           commit('SET_MENUList', res.data.menuList)
    //         } else {
    //           reject('getInfo: roles must be a non-null array !')
    //         }
    //         commit('SET_NAME', res.data.yktUserInfo.name)
    //         commit('SET_BASICINFO', res.data)
    //         sesto.setItem('allInfo', res.data)
    //         resolve(res)
    //       })
    //     }

    //     if (state === 2) {
    //       getInfo(state).then(res => {
    //         commit('SET_OTHERINFO', res.data)
    //         resolve(res)
    //       })
    //     }
    //   })
    // },

    // 登出 -- 清除cookies和store中的token, 并重置角色列表
    LogOut({
      commit
    }, {
      token
    }) {
      return new Promise((resolve, reject) => {
        logout(token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_MENUList', [])
          // removeToken()
          sessionStorage.clear() // 清除一下sessionStorage信息
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户课程信息
    GetCourseInfo({
      commit
    }) {
      return new Promise((resolve, reject) => {
        request.get('/api/other/getInfo/3').then(res => {
          commit('SET_COURSEINFO', res.data)
          resolve(res)
        }).catch(error => {
          commit('SET_COURSEINFO', {})
          reject(error)
        })
      })
    },

    // 前端 登出 -- 清除cookies和store中的token
    FedLogOut({
      commit
    }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        // removeToken()
        resolve()
      })
    }
  }
}

export default user
